package org.pim.smart.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.pim.smart.hibernate.domain.ContactMain;
import org.pim.smart.hibernate.domain.ContactTag;
import org.pim.smart.hibernate.domain.GroupContact;
import org.pim.smart.hibernate.domain.SysUser;
import org.pim.smart.hibernate.domain.SysUserDAO;
import org.pim.smart.hibernate.domain.TagContactMap;
import org.pim.smart.hibernate.domain.TagContactMapDAO;
import org.pim.smart.hibernate.domain.TagContactMapId;

public class TagContactMapData {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SysUserDAO userDao = new SysUserDAO();
		TagContactMapDAO mapDao = new TagContactMapDAO();
		List<SysUser> users = userDao.findByExample(new SysUser());
		for (Iterator<SysUser> it = users.iterator(); it.hasNext(); ){
			SysUser user = it.next();
			Set<ContactTag> tags = user.getContactTags();
			List<ContactTag> tagList = new ArrayList<ContactTag>(tags);
			for (Iterator<GroupContact> itc = user.getGroupContacts().iterator(); itc.hasNext();){
				GroupContact group = itc.next();
				for (Iterator<ContactMain> itm = group.getContactMains().iterator(); itm.hasNext();){
					ContactMain contact = itm.next();
					if (contact.getContactMainOid().intValue()%2 == 0){
						TagContactMap map = new TagContactMap();
						TagContactMapId id = new TagContactMapId();
						map.setContactMain(contact);
						map.setContactTag(tagList.get(1));
						id.setContactMainOid(contact.getContactMainOid());
						id.setTagOid(tagList.get(1).getTagOid());
						map.setId(id);
						mapDao.save(map);
						TagContactMap map1 = new TagContactMap();
						TagContactMapId id1 = new TagContactMapId();
						map1.setContactMain(contact);
						map1.setContactTag(tagList.get(3));
						id1.setContactMainOid(contact.getContactMainOid());
						id1.setTagOid(tagList.get(3).getTagOid());
						map1.setId(id1);
						mapDao.save(map1);
					} else {
						TagContactMap map = new TagContactMap();
						TagContactMapId id = new TagContactMapId();
						map.setContactMain(contact);
						map.setContactTag(tagList.get(0));
						id.setContactMainOid(contact.getContactMainOid());
						id.setTagOid(tagList.get(0).getTagOid());
						map.setId(id);
						mapDao.save(map);
						TagContactMap map1 = new TagContactMap();
						TagContactMapId id1 = new TagContactMapId();
						map1.setContactMain(contact);
						map1.setContactTag(tagList.get(2));
						id1.setContactMainOid(contact.getContactMainOid());
						id1.setTagOid(tagList.get(2).getTagOid());
						map1.setId(id1);
						mapDao.save(map1);
					}
				}
			}
		}
		mapDao.getSession().beginTransaction().commit();
	}

}
